iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 20
0
AI & Data

量化投資與機器學習研究系列 第 20

20.找出隔日沖分點改進+這半年來的妖股

  • 分享至 

  • xImage
  •  

上一篇前40名分點出來,覺得怪怪的,我所熟知的很強的隔日沖分點排名很後面
隔日沖很大一個特色是鎖漲停
所以我們改一下定義

加上股價的資訊

with MongoDatabase('stock_day') as conn:
    stock = conn.export_df({'date':{'$gte':'2018-06-15'},'成交股數':{'$gte':300}},{'date':1,'name':1,'code':1,"開盤價":1,"最高價":1,"最低價":1,"收盤價":1,"成交筆數":1 })

with MongoDatabase('broker_info') as conn:
    data = conn.export_df({'$or':[{'差額張數':{'$gte':150}},{'差額張數':{'$lte':-150}}]})

data1=pd.merge(data,stock[['code', 'date', '開盤價', '最高價', '最低價', '收盤價', '成交筆數']],on=['date','code'])

rolling跟一般的apply不一樣,只能有一個變數做窗口,網路上有高手
用了很有創意的想法

加上買的當天要接近漲停(9%以上)才是隔日沖

def next_day_sell(ii,d):
    d = d.iloc[int(min(ii)):int(max(ii))+1]
    if d.iloc[0]['差額張數']>0:
        if d.iloc[1]['差額張數']<0 and abs(d.iloc[1]['差額張數'])/d.iloc[0]['差額張數']>=0.6 and d.iloc[0]['最高價']>d.iloc[0]['開盤價']*1.09:
            return 1
        return 0
    return 0


from collections import defaultdict
ndd = defaultdict(int)
ncd = defaultdict(int)

for r in data1.groupby(['分點代號','code'])[['date','差額張數','最高價','開盤價']]:
    d=r[1].set_index('date')
    d['ii'] = range(len(d))
    
    count=d['ii'].rolling(window='2d',min_periods=2).agg(lambda x: next_day_sell(x, d)).sum()
    ndd[r[0][0]]+=count
    ncd[r[0][1]]+=count

效果好很多

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>卷商代號</th>
      <th>卷商</th>
      <th>分點代號</th>
      <th>分點</th>
      <th>次數</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>34</th>
      <td>1440</td>
      <td>美林</td>
      <td>1440</td>
      <td>美林</td>
      <td>62.0</td>
    </tr>
    <tr>
      <th>26</th>
      <td>9800</td>
      <td>元大</td>
      <td>987D</td>
      <td>元大-新壢</td>
      <td>17.0</td>
    </tr>
    <tr>
      <th>8</th>
      <td>5850</td>
      <td>統一</td>
      <td>585U</td>
      <td>統一-南京</td>
      <td>13.0</td>
    </tr>
    <tr>
      <th>0</th>
      <td>9800</td>
      <td>元大</td>
      <td>9800</td>
      <td>元大</td>
      <td>11.0</td>
    </tr>
    <tr>
      <th>27</th>
      <td>5920</td>
      <td>元富</td>
      <td>592B</td>
      <td>元富-永和</td>
      <td>10.0</td>
    </tr>
    <tr>
      <th>7</th>
      <td>5850</td>
      <td>統一</td>
      <td>585P</td>
      <td>統一-板橋</td>
      <td>8.0</td>
    </tr>
    <tr>
      <th>10</th>
      <td>5850</td>
      <td>統一</td>
      <td>585c</td>
      <td>統一-仁愛</td>
      <td>8.0</td>
    </tr>
    <tr>
      <th>1</th>
      <td>8770</td>
      <td>大鼎</td>
      <td>8770</td>
      <td>大鼎</td>
      <td>8.0</td>
    </tr>
    <tr>
      <th>31</th>
      <td>7000</td>
      <td>兆豐</td>
      <td>7000</td>
      <td>兆豐</td>
      <td>8.0</td>
    </tr>
    <tr>
      <th>15</th>
      <td>5850</td>
      <td>統一</td>
      <td>5850</td>
      <td>統一</td>
      <td>7.0</td>
    </tr>
    <tr>
      <th>9</th>
      <td>5850</td>
      <td>統一</td>
      <td>585M</td>
      <td>統一-士林</td>
      <td>7.0</td>
    </tr>
    <tr>
      <th>33</th>
      <td>7000</td>
      <td>兆豐</td>
      <td>700W</td>
      <td>兆豐-城中</td>
      <td>6.0</td>
    </tr>
    <tr>
      <th>30</th>
      <td>9800</td>
      <td>元大</td>
      <td>980K</td>
      <td>元大-竹科</td>
      <td>6.0</td>
    </tr>
    <tr>
      <th>21</th>
      <td>9600</td>
      <td>富邦</td>
      <td>9697</td>
      <td>富邦-虎尾</td>
      <td>5.0</td>
    </tr>
    <tr>
      <th>32</th>
      <td>7000</td>
      <td>兆豐</td>
      <td>700F</td>
      <td>兆豐-虎尾</td>
      <td>5.0</td>
    </tr>
    <tr>
      <th>14</th>
      <td>1480</td>
      <td>美商高盛</td>
      <td>1480</td>
      <td>美商高盛</td>
      <td>5.0</td>
    </tr>
    <tr>
      <th>19</th>
      <td>9300</td>
      <td>華南永昌</td>
      <td>9326</td>
      <td>華南永昌-南京</td>
      <td>4.0</td>
    </tr>
    <tr>
      <th>22</th>
      <td>9600</td>
      <td>富邦</td>
      <td>9658</td>
      <td>富邦-建國</td>
      <td>4.0</td>
    </tr>
    <tr>
      <th>35</th>
      <td>1470</td>
      <td>台灣摩根士丹利</td>
      <td>1470</td>
      <td>台灣摩根士丹利</td>
      <td>4.0</td>
    </tr>
    <tr>
      <th>18</th>
      <td>1520</td>
      <td>瑞士信貸</td>
      <td>1520</td>
      <td>瑞士信貸</td>
      <td>4.0</td>
    </tr>
    <tr>
      <th>38</th>
      <td>9A00</td>
      <td>永豐金</td>
      <td>9A9C</td>
      <td>永豐金-員林</td>
      <td>4.0</td>
    </tr>
    <tr>
      <th>11</th>
      <td>9200</td>
      <td>凱基</td>
      <td>9239</td>
      <td>凱基-市政</td>
      <td>4.0</td>
    </tr>
    <tr>
      <th>12</th>
      <td>8880</td>
      <td>國泰</td>
      <td>888K</td>
      <td>國泰-板橋</td>
      <td>4.0</td>
    </tr>
    <tr>
      <th>36</th>
      <td>9A00</td>
      <td>永豐金</td>
      <td>9A00</td>
      <td>永豐金</td>
      <td>3.0</td>
    </tr>
    <tr>
      <th>29</th>
      <td>1160</td>
      <td>日盛</td>
      <td>1160</td>
      <td>日盛</td>
      <td>3.0</td>
    </tr>
    <tr>
      <th>20</th>
      <td>9100</td>
      <td>群益金鼎</td>
      <td>918J</td>
      <td>群益金鼎-永和</td>
      <td>3.0</td>
    </tr>
    <tr>
      <th>25</th>
      <td>5920</td>
      <td>元富</td>
      <td>5920</td>
      <td>元富</td>
      <td>3.0</td>
    </tr>
    <tr>
      <th>23</th>
      <td>1530</td>
      <td>港商德意志</td>
      <td>1530</td>
      <td>港商德意志</td>
      <td>3.0</td>
    </tr>
    <tr>
      <th>17</th>
      <td>7790</td>
      <td>國票</td>
      <td>779W</td>
      <td>國票-彰化</td>
      <td>3.0</td>
    </tr>
    <tr>
      <th>16</th>
      <td>7790</td>
      <td>國票</td>
      <td>779c</td>
      <td>國票-敦北法人</td>
      <td>3.0</td>
    </tr>
    <tr>
      <th>6</th>
      <td>5850</td>
      <td>統一</td>
      <td>585B</td>
      <td>統一-永和</td>
      <td>3.0</td>
    </tr>
    <tr>
      <th>5</th>
      <td>9200</td>
      <td>凱基</td>
      <td>9218</td>
      <td>凱基-大直</td>
      <td>3.0</td>
    </tr>
    <tr>
      <th>4</th>
      <td>9200</td>
      <td>凱基</td>
      <td>9268</td>
      <td>凱基-台北</td>
      <td>3.0</td>
    </tr>
    <tr>
      <th>3</th>
      <td>9800</td>
      <td>元大</td>
      <td>9875</td>
      <td>元大-土城永寧</td>
      <td>3.0</td>
    </tr>
    <tr>
      <th>39</th>
      <td>9800</td>
      <td>元大</td>
      <td>983B</td>
      <td>元大-林森</td>
      <td>3.0</td>
    </tr>
    <tr>
      <th>24</th>
      <td>9300</td>
      <td>華南永昌</td>
      <td>9359</td>
      <td>華南永昌-中正</td>
      <td>2.0</td>
    </tr>
    <tr>
      <th>28</th>
      <td>1160</td>
      <td>日盛</td>
      <td>1161</td>
      <td>日盛-忠孝</td>
      <td>2.0</td>
    </tr>
    <tr>
      <th>13</th>
      <td>7790</td>
      <td>國票</td>
      <td>779u</td>
      <td>國票-長城</td>
      <td>2.0</td>
    </tr>
    <tr>
      <th>37</th>
      <td>9A00</td>
      <td>永豐金</td>
      <td>9A92</td>
      <td>永豐金-萬盛</td>
      <td>2.0</td>
    </tr>
    <tr>
      <th>2</th>
      <td>5050</td>
      <td>大展</td>
      <td>5050</td>
      <td>大展</td>
      <td>2.0</td>
    </tr>
  </tbody>
</table>

妖股第一名感覺不意外,遠遠超越第二名,鎖完漲停隔天就賣超過6成的危險股票
由於用張數150過濾,會過濾掉一些高價股,未來要再改進

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>code</th>
      <th>name</th>
      <th>次數</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>2</th>
      <td>2349</td>
      <td>錸德</td>
      <td>77.0</td>
    </tr>
    <tr>
      <th>16</th>
      <td>5469</td>
      <td>瀚宇博</td>
      <td>15.0</td>
    </tr>
    <tr>
      <th>26</th>
      <td>3360</td>
      <td>尚立</td>
      <td>12.0</td>
    </tr>
    <tr>
      <th>27</th>
      <td>2486</td>
      <td>一詮</td>
      <td>11.0</td>
    </tr>
    <tr>
      <th>18</th>
      <td>4944</td>
      <td>兆遠</td>
      <td>11.0</td>
    </tr>
    <tr>
      <th>30</th>
      <td>6509</td>
      <td>聚和</td>
      <td>9.0</td>
    </tr>
    <tr>
      <th>1</th>
      <td>2363</td>
      <td>矽統</td>
      <td>9.0</td>
    </tr>
    <tr>
      <th>5</th>
      <td>2323</td>
      <td>中環</td>
      <td>8.0</td>
    </tr>
    <tr>
      <th>32</th>
      <td>6278</td>
      <td>台表科</td>
      <td>8.0</td>
    </tr>
    <tr>
      <th>12</th>
      <td>6120</td>
      <td>達運</td>
      <td>8.0</td>
    </tr>
    <tr>
      <th>24</th>
      <td>2425</td>
      <td>承啟</td>
      <td>7.0</td>
    </tr>
    <tr>
      <th>14</th>
      <td>2329</td>
      <td>華泰</td>
      <td>7.0</td>
    </tr>
    <tr>
      <th>39</th>
      <td>8028</td>
      <td>昇陽半導體</td>
      <td>7.0</td>
    </tr>
    <tr>
      <th>29</th>
      <td>2442</td>
      <td>新美齊</td>
      <td>5.0</td>
    </tr>
    <tr>
      <th>28</th>
      <td>2368</td>
      <td>金像電</td>
      <td>5.0</td>
    </tr>
    <tr>
      <th>22</th>
      <td>6191</td>
      <td>精成科</td>
      <td>5.0</td>
    </tr>
    <tr>
      <th>21</th>
      <td>4725</td>
      <td>信昌化</td>
      <td>5.0</td>
    </tr>
    <tr>
      <th>38</th>
      <td>2390</td>
      <td>云辰</td>
      <td>5.0</td>
    </tr>
    <tr>
      <th>0</th>
      <td>6116</td>
      <td>彩晶</td>
      <td>5.0</td>
    </tr>
    <tr>
      <th>19</th>
      <td>8040</td>
      <td>九暘</td>
      <td>4.0</td>
    </tr>
    <tr>
      <th>25</th>
      <td>6113</td>
      <td>亞矽</td>
      <td>4.0</td>
    </tr>
    <tr>
      <th>6</th>
      <td>1909</td>
      <td>榮成</td>
      <td>4.0</td>
    </tr>
    <tr>
      <th>17</th>
      <td>8111</td>
      <td>立碁</td>
      <td>4.0</td>
    </tr>
    <tr>
      <th>9</th>
      <td>5410</td>
      <td>國眾</td>
      <td>4.0</td>
    </tr>
    <tr>
      <th>7</th>
      <td>3630</td>
      <td>新鉅科</td>
      <td>4.0</td>
    </tr>
    <tr>
      <th>23</th>
      <td>5425</td>
      <td>台半</td>
      <td>4.0</td>
    </tr>
    <tr>
      <th>13</th>
      <td>3591</td>
      <td>艾笛森</td>
      <td>4.0</td>
    </tr>
    <tr>
      <th>10</th>
      <td>1312</td>
      <td>國喬</td>
      <td>3.0</td>
    </tr>
    <tr>
      <th>8</th>
      <td>3576</td>
      <td>新日光</td>
      <td>3.0</td>
    </tr>
    <tr>
      <th>15</th>
      <td>6244</td>
      <td>茂迪</td>
      <td>3.0</td>
    </tr>
    <tr>
      <th>31</th>
      <td>4426</td>
      <td>利勤</td>
      <td>3.0</td>
    </tr>
    <tr>
      <th>33</th>
      <td>6167</td>
      <td>久正</td>
      <td>3.0</td>
    </tr>
    <tr>
      <th>34</th>
      <td>3221</td>
      <td>台嘉碩</td>
      <td>3.0</td>
    </tr>
    <tr>
      <th>35</th>
      <td>4989</td>
      <td>榮科</td>
      <td>3.0</td>
    </tr>
    <tr>
      <th>36</th>
      <td>1539</td>
      <td>巨庭</td>
      <td>3.0</td>
    </tr>
    <tr>
      <th>37</th>
      <td>5464</td>
      <td>霖宏</td>
      <td>3.0</td>
    </tr>
    <tr>
      <th>3</th>
      <td>00655L</td>
      <td>國泰中國A50正2</td>
      <td>3.0</td>
    </tr>
    <tr>
      <th>20</th>
      <td>6141</td>
      <td>柏承</td>
      <td>3.0</td>
    </tr>
    <tr>
      <th>11</th>
      <td>6213</td>
      <td>聯茂</td>
      <td>2.0</td>
    </tr>
    <tr>
      <th>4</th>
      <td>2456</td>
      <td>奇力新</td>
      <td>2.0</td>
    </tr>
  </tbody>
</table>

上一篇
19.找出隔日沖分點
下一篇
21.第一次炒股就上手
系列文
量化投資與機器學習研究30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言